#region Disclaimer by Teo Lachev
/*============================================================================
  The CustomDataExtension sample uses code from the Microsoft File Share Data 
  Processing Extension Sample (http://msdn2.microsoft.com/en-us/library/ms160918.aspx). 
  The orginal code base has been modified whenever needed to re-purpose the 
  extension to support ADO.NET datasets. Most code changes are implemented in the 
  DsDataReader class.
===========================================================================*/
#endregion

using System;
using System.ComponentModel;
using Microsoft.ReportingServices.DataProcessing;
using System.Diagnostics;

namespace Insight.RS.Extensibility.CustomDataExtension
{
    /// <summary>
    /// Represents a command
    /// </summary>
	public class DsCommand : IDbCommand, IDbCommandAnalysis
	{
		DsConnectionWrapper			m_connection;
		DsTransaction				m_txn;
		string						m_cmdText;
		DsDataParameterCollection m_parameters = new DsDataParameterCollection();
     
		public DsCommand()
		{
			// Implement the default constructor here
		}

		// Implement other constructors here.
		public DsCommand(DsConnectionWrapper connection)
		{
			m_connection = connection;
		}

		public DsCommand(string cmdText)
		{
			m_cmdText = cmdText;
		}

		public DsCommand(string cmdText, DsConnectionWrapper connection)
		{
			m_cmdText    = cmdText;
			m_connection  = connection;
		}

		public DsCommand(string cmdText, DsConnectionWrapper connection, DsTransaction txn)
		{
			m_cmdText    = cmdText;
			m_connection  = connection;
			m_txn      = txn;
		}

		/****
		* IMPLEMENT THE REQUIRED PROPERTIES.
		****/
		/// <summary>
		/// Implements IDbCommandAnalysis.GetParameters
		/// GetParameters is called by the Report Designer
		/// </summary>
		/// <returns></returns>
		public IDataParameterCollection GetParameters ()
		{
			DsDataParameterCollection parameters = new DsDataParameterCollection();
			// inform the designer that @DataSource parameter is needed
			parameters.Add(new DsDataParameter(Util.DATA_SOURCE, m_connection.ConnectionString));
			return parameters;
		}
		public string CommandText
		{
			get { return m_cmdText;  }
			set  { m_cmdText = value;  }
		}

		public int CommandTimeout
		{
			/*
			* The sample does not support a command time-out. 
			*/
			get  { return 30; }
			set  {}
		}

		public CommandType CommandType
		{
			/*
			* The sample only supports CommandType.Text.
			*/
			get { return CommandType.Text; }
			set { if (value != CommandType.Text) throw new NotSupportedException(); }
		}


		public DsDataParameterCollection Parameters
		{
			get  { return m_parameters; }
		}

		IDataParameterCollection IDbCommand.Parameters
		{
			get  { return m_parameters; }
		}

		public IDbTransaction Transaction
		{
			/*
			* Set the transaction. Consider additional steps to ensure that the transaction
			* is compatible with the connection, because the two are usually linked.
			*/
			get { return m_txn; }
			set { m_txn = (DsTransaction)value; }
		}

      /****
      * IMPLEMENT THE REQUIRED METHODS.
      ****/

      public void Cancel()
      {
         // The sample does not support canceling a command
         // once it has been initiated.
         throw new NotSupportedException();
      }

      public IDataParameter CreateParameter()
      {
         return (IDataParameter)(new DsDataParameter());
      }

      public IDataReader ExecuteReader()
      {
         /*
         * ExecuteReader should retrieve results from the data source
         * and return a DataReader that allows the user to process 
         * the results.
         */
         // There must be a valid and open connection.
         // must have a valid and open connection 
		  Trace.WriteLine("ExecuteReader");
         if (m_connection == null || m_connection.State != System.Data.ConnectionState.Open) 
            throw new InvalidOperationException("Connection must be valid and open.");

         // Execute the command.
         DsDataReader reader = new DsDataReader(m_connection, m_cmdText, m_parameters);
         reader.LoadDataset();
         
         return reader;
      }

      public IDataReader ExecuteReader(CommandBehavior behavior)
      {
         /*
         * ExecuteReader should retrieve results from the data source
         * and return a DataReader that allows the user to process 
         * the results.
         */
		Trace.WriteLine("ExecuteReader");
		  
         // There must be a valid and open connection.
         if (m_connection == null || m_connection.State != System.Data.ConnectionState.Open) 
            throw new InvalidOperationException("Connection must be valid and open.");

         // Execute the command.      
         DsDataReader reader = new DsDataReader(m_connection, m_cmdText, m_parameters);
         reader.LoadDataset();

         return reader;
      }

      public void Dispose() 
      {
         /*
         * Dispose of the object and perform any cleanup.
         */
      }
   }
}

